<?php if (!defined('BASEPATH')) exit('No direct script access allowed');


class Reports_sms_model extends CI_Model
{

	public $reports_sms ;
	private $CI;
	
	public function __construct()
	{
		parent::__construct();
		$this->CI =& get_instance();
	}
	public function getCount($filters = array()){
		return $this->getData($filters,true);
	}
	
	
	public function getData($filters = array(),$count = false){
		
		if (isset($this->CI->cache)) {
			$caching = TRUE;
			$cache_key = 'ci---REPORTS_' . md5(serialize($filters));
			
			if ($counting == TRUE) {
				$cache_key = 'ci---REPORTS_COUNT_' . md5(serialize($filters));
			}
			
			
			if ($return = $this->cache->file->get($cache_key)) {
				return ($return == 'empty_cache') ? FALSE : $return;
			}
		}
		else {
			$caching = FALSE;
		}
		
		$this->db->from('manager_users_sms');
		if (isset($filters['userid'])){
			$this->db->where('userid',$filters['userid']);
		}
		if (isset($filters['sort'])){
			$this->db->order_by($filters['sort'],$filters['dir']);
		}
		if (isset($filters['limit'])) {
			$offset = (isset($filters['offset'])) ? $filters['offset'] : 0;
			$this->db->limit($filters['limit'], $offset);
		}
		
		$result = $this->db->get();
		
		if($count){
			$rows = $result->num_rows();
			$result->free_result();
			if ($caching == TRUE) {
				$this->CI->cache->file->save($cache_key, $rows, (5*60));	
			}
			return $rows;
		}
		
		if ($result->num_rows() == 0) {
			return FALSE;
		}
		$reports_sms = array();
		foreach ($result->result_array() as $row) {
				$reports_sms[] = array(
							'id' => $row['id'],
							'userid' => $row['userid'],
							'datetime' => $row['datetime'],
							'syntax' => $row['syntax'],
							'service'=>$row['service'],
							'money'=>$row['money']
						);
			}
		if ($caching == TRUE) {
			$this->CI->cache->file->save($cache_key, $reports_sms, (5*60));	
		}
		$result->free_result();
		return $reports_sms;
	}
}